package com.beemans.common.ext

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import androidx.fragment.app.Fragment
import com.beemans.common.utils.AutoSize
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.gyf.immersionbar.ImmersionBar
import kotlin.math.roundToInt

/**
 * @author tiamosu
 * @date 2021/3/12
 */
/**
 * 获取应用屏幕的宽度，单位px
 */
val appScreenWidth by lazy { ScreenUtils.getAppScreenWidth() }

/**
 * 获取应用屏幕的高度（单位：px）
 */
val appScreenHeight by lazy { ScreenUtils.getAppScreenHeight() }

/**
 * 获取状态栏高度
 */
val Context.getStatusBarHeight: Int
    get() = when (this) {
        is Activity -> ImmersionBar.getStatusBarHeight(this)
        is Fragment -> ImmersionBar.getStatusBarHeight(this)
        else -> {   //进行屏幕适配后，调用BarUtils.getStatusBarHeight()获取状态栏高度不正确
            val resources = Resources.getSystem()
            val resourceId = resources?.getIdentifier("status_bar_height", "dimen", "android")
            resourceId?.let { resources.getDimensionPixelSize(it) } ?: BarUtils.getStatusBarHeight()
        }
    }

val Float.pt2px: Float
    get() = appScreenWidth / AutoSize.DESIGN_WIDTH * this

val Int.pt2px: Int
    get() = this.toFloat().pt2px.roundToInt()

val Float.pt2dp: Int
    get() = SizeUtils.px2dp(this.pt2px)

val Int.pt2dp: Int
    get() = this.toFloat().pt2dp

val Float.px2dp: Int
    get() = SizeUtils.px2dp(this)

val Int.px2dp: Int
    get() = this.toFloat().px2dp